<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import useClipboard from 'vue-clipboard3'
import { GetData, PatchModifyType } from '@/api/modules/drawact'
import GetCacheData from '@/store/modules/NewPageCache'

const imgDomain = 'https://img1.eprhan.cc'
const router = useRouter()
const cacheData = GetCacheData()
let PageNum = cacheData.GetCacheData('drawact').currentPage

const findFrom = ref({
  ...(cacheData.GetCacheData('drawact').fromData),
})
const dataList: any = reactive({
  list: [],
})
const loading = ref(true)
const total = ref(0)

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.ActStatus && ({ ActStatus: findFrom.value.ActStatus })),
  }
  await GetData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
  })
  loading.value = false
}
// 查询
function Inquire() {
  GetDataList(1, cacheData.GetCacheData('drawact').currentSize)
  cacheData.SetCacheData('drawact', PageNum, cacheData.GetCacheData('drawact').currentSize, findFrom.value)
}
// 修改状态
async function ModifyType(params: any) {
  // const params = {
  //   actDrawId: 1000100,
  //   actStatus: 1,
  // }
  await PatchModifyType(params).then((res: any) => {
    if (res.statusCode === 200) {
      ElMessage.success('修改成功')
      GetDataList(cacheData.GetCacheData('drawact').currentPage, cacheData.GetCacheData('drawact').currentSize)
    }
    else {
      ElMessage.error(`修改失败${res.errors}`)
    }
  })
}
// 添加
function AddData() {
  router.push({
    name: 'drawactDetail',
  })
}
// 编辑
function EditData(id: number) {
  router.push({
    name: 'drawactDetail',
    query: {
      id,
    },
  })
}

// 复制
async function CopyUrl(id: number) {
  const clipboard = useClipboard()
  try {
    // 使用 navigator.clipboard.writeText 来复制文本
    await clipboard.toClipboard(`/pages/lottery/lottery?DrawId=${id}`)
    ElMessage.success('复制成功')
  }
  catch (error) {
    const textarea = document.createElement('textarea')
    textarea.value = `/pages/lottery/lottery?DrawId=${id}`
    document.body.appendChild(textarea)
    textarea.select()
    // 使用现代浏览器支持的复制命令
    const successful = document.execCommand('copy')
    if (!successful) {
      ElMessage.error('复制失败')
    }
    else {
      ElMessage.success('复制成功')
    }
    document.body.removeChild(textarea)
  }
}

// 查看抽奖记录
function ExamineRecord(id: number) {
  localStorage.setItem('currentPage', `${PageNum}`)
  router.push({
    name: 'drawactRecord',
    query: {
      DrawId: id,
    },
  })
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  cacheData.SetCacheData('drawact', PageNum, val)
}
function handleCurrentChange(val: number) {
  GetDataList(val, cacheData.GetCacheData('drawact').currentSize)
  cacheData.SetCacheData('drawact', val, cacheData.GetCacheData('drawact').currentSize)
  PageNum = val
}

const nowPageNum = computed(() => {
  return cacheData.GetCacheData('drawact').currentPage || 1
})

onMounted(async () => {
  GetDataList(cacheData.GetCacheData('drawact').currentPage, cacheData.GetCacheData('drawact').currentSize)
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="活动状态">
                <el-select v-model="findFrom.ActStatus" placeholder="请选择" clearable>
                  <el-option label="启用" :value="1" />
                  <el-option label="禁用" :value="-1" />
                </el-select>
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <el-button type="success" @click="AddData">
          添加
        </el-button>
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="drawId" label="活动ID" width="100" />
        <el-table-column align="center" prop="beginTime" label="活动开始时间" width="150">
          <template #default="scope">
            <p class="m-0">
              {{ scope.row.beginTime.split(' ')[0] }}
            </p>
            <p class="m-0">
              {{ scope.row.beginTime.split(' ')[1] }}
            </p>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="endTime" label="活动结束时间" width="150">
          <template #default="scope">
            <p class="m-0">
              {{ scope.row.endTime.split(' ')[0] }}
            </p>
            <p class="m-0">
              {{ scope.row.endTime.split(' ')[1] }}
            </p>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="moreLink" label="更多抽奖机会链接" width="350" />
        <el-table-column header-align="center" prop="shareTxt" label="分享语" width="400" />
        <!-- <el-table-column align="center" prop="actStatus" label="活动状态" width="160">
          <template #default="scope">
            <el-tag>
              {{ scope.row.actStatus === 1 ? '启用'
                : scope.row.actStatus === -1 ? '结束' : '未知' }}
            </el-tag>
          </template>
        </el-table-column> -->
        <el-table-column align="center" prop="bgColor" label="页面背景色" width="150">
          <template #default="scope">
            <el-color-picker v-model="scope.row.bgColor" disabled />
            <p>{{ scope.row.bgColor }}</p>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="btnBgColor" label="按钮文字色值" width="150">
          <template #default="scope">
            <el-color-picker v-model="scope.row.btnBgColor" disabled />
            <p>{{ scope.row.btnBgColor }}</p>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="getPrizeBtnColor" label="奖品按钮文字色值" width="150">
          <template #default="scope">
            <el-color-picker v-model="scope.row.getPrizeBtnColor" disabled />
            <p>{{ scope.row.getPrizeBtnColor }}</p>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="getPrizeBgbtnColor" label="奖品按钮背景色值" width="150">
          <template #default="scope">
            <el-color-picker v-model="scope.row.getPrizeBgbtnColor" disabled />
            <p>{{ scope.row.getPrizeBgbtnColor }}</p>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="descriptionTxt" label="使用说明文案" width="150" />
        <el-table-column align="center" label="使用说明图片" width="150">
          <template #default="scope">
            <el-avatar shape="square" :size="80" :src="imgDomain + scope.row.descriptionPic" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="活动分享图" width="150">
          <template #default="scope">
            <el-avatar shape="square" :size="80" :src="imgDomain + scope.row.sharePic" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="活动背景图" width="150">
          <template #default="scope">
            <el-avatar shape="square" :size="80" :src="imgDomain + scope.row.drawBg" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="转盘图" width="150">
          <template #default="scope">
            <el-avatar shape="square" :size="80" :src="imgDomain + scope.row.wheelBg" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="活动前景图" width="150">
          <template #default="scope">
            <el-avatar shape="square" :size="80" :src="imgDomain + scope.row.drawPrebg" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="抽奖按钮图" width="150">
          <template #default="scope">
            <el-avatar shape="square" :size="80" :src="imgDomain + scope.row.btnBg" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="规则图标" width="150">
          <template #default="scope">
            <el-avatar shape="square" :size="80" :src="imgDomain + scope.row.ruleIcon" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="规则图" width="150">
          <template #default="scope">
            <el-avatar shape="square" :size="80" :src="imgDomain + scope.row.rulePic" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="奖品背景图" width="150">
          <template #default="scope">
            <el-avatar shape="square" :size="80" :src="imgDomain + scope.row.myprizeBg" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="奖品图标" width="150">
          <template #default="scope">
            <el-avatar shape="square" :size="80" :src="imgDomain + scope.row.myprizeIcon" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="我的奖品券背景图" width="150">
          <template #default="scope">
            <el-avatar shape="square" :size="80" :src="imgDomain + scope.row.myprizeTicketbg" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="更多抽奖机会图标" width="150">
          <template #default="scope">
            <el-avatar shape="square" :size="80" :src="imgDomain + scope.row.moreIcon" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否启用" fixed="right" width="120">
          <template #default="scope">
            <el-switch
              v-model="scope.row.actStatus"
              :active-value="1"
              :inactive-value="-1"
              @click="ModifyType({ id: scope.row.drawId, actStatus: scope.row.actStatus })"
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right" min-width="300">
          <template #default="scope">
            <el-button type="success" @click="ExamineRecord(scope.row.drawId)">
              抽奖记录
            </el-button>
            <el-button type="primary" @click="EditData(scope.row.drawId)">
              编辑
            </el-button>
            <el-button type="info" @click="CopyUrl(scope.row.drawId)">
              复制路径
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="cacheData.GetCacheData('drawact').currentSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}
</style>
